package IOperation;


import book.Book;
import book.Booklist;

import java.util.Scanner;

public class DleBook implements ioperation{
    @Override
    public void work(Booklist booklist) {

        //输入删除的书籍
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入书籍名称：>");
        String bookname = scanner.nextLine();

        //遍历数组查找
        Book[] book = booklist.getBooks();
        int i = 0;
        for (; i < book.length; i++) {
            Book book1 = booklist.getBooks(i);
            if(book1.getBookname().equals(bookname))
                break;
        }
        //找不到
        if( i == book.length) {
            System.out.println("输入的书籍名称不存在！！！");
            return ;
        }
        //找到了：使用循环覆盖
        int j = 0;
        for (j = i; j < book.length - 1; j++) {
            //将数组前一个元素覆盖到前一个元素
            Book tembook = booklist.getBooks(j + 1);
            booklist.setBooks(j,tembook);
        }
        //此时还剩最末尾的元素占用空间，将其设置为null
        booklist.setBooks(j,null);
        //最后将书籍数量减一
        booklist.setBookCount(booklist.getBookCount() - 1);
        //提示一下
        System.out.println(bookname + "此书籍删除成功");
    }
}
